package com.siping.算法.红包;

import java.util.List;
import java.util.Random;

/**
 * @author YangXu
 * @date 2022/9/22
 * @description:
 */
public class RedPackageDemo {

    public static void main(String[] args) {
        int remainSize = 5;
        RedPackage redPackage = new RedPackage(remainSize, 100);
        for (int i = 0; i < remainSize; i++) {
            double randomMoney = getRandomMoney(redPackage);
            System.out.println(randomMoney);
        }
    }

    public static double getRandomMoney(RedPackage redPackage) {
        if (redPackage.remainSize == 1) {
            redPackage.remainSize--;
            return (double) Math.round(redPackage.remainMoney * 100) / 100;
        }

        Random r = new Random();
        double min = 0.01;
        double max = redPackage.remainMoney / redPackage.remainSize * 2;
        double money = r.nextDouble() * max;
        money = money <= min ? 0.01 : money;
        money = Math.floor(money * 100) / 100;
        redPackage.remainSize--;
        redPackage.remainMoney -= money;
        return money;

    }

    public static double rand(double money, int people, List<Double> l) {

        if (people == 1) {

            double red = Math.round(money * 100) / 100.0;

            l.add(red + 0.01);

            return 0;

        }

        Random random = new Random();

        double min = 0;

        double max = money / people * 2.0;

        double red = random.nextDouble() * max;

        red = red <= min ? min : red;

        red = Math.floor(red * 100) / 100.0;

        l.add(red + 0.01);

        double remain = Math.round((money - red) * 100) / 100.0;

        return remain;

    }

    static class RedPackage {
        // 剩余的红包数量
        int remainSize;
        // 剩余的钱
        double remainMoney;

        public RedPackage(int remainSize, double remainMoney) {
            this.remainSize = remainSize;
            this.remainMoney = remainMoney;
        }

        public int getRemainSize() {
            return remainSize;
        }

        public void setRemainSize(int remainSize) {
            this.remainSize = remainSize;
        }

        public double getRemainMoney() {
            return remainMoney;
        }

        public void setRemainMoney(double remainMoney) {
            this.remainMoney = remainMoney;
        }
    }
}
